package com.briup.jd2511.jd2511_product_source.controller;

import com.briup.jd2511.jd2511_product_source.exception.ServiceException;
import com.briup.jd2511.jd2511_product_source.pojo.ManagerFenceHouse;
import com.briup.jd2511.jd2511_product_source.result.Result;
import com.briup.jd2511.jd2511_product_source.service.FenceHouseService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Parameters;
import io.swagger.v3.oas.annotations.enums.ParameterIn;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Tag(name = "栏舍管理模块")
@RestController
@RequestMapping("/fenceHouse")
public class FenceHouseController {

    @Autowired
    private FenceHouseService fenceHouseService;

    @GetMapping
    @Operation(summary = "分页/条件查询栏舍")
    @Parameters({
            @Parameter(name = "fhName",description = "栏舍名称",in = ParameterIn.QUERY),
            @Parameter(name = "pageNum",description = "当前页码",in = ParameterIn.QUERY),
            @Parameter(name = "pageSize",description = "每页大小",in = ParameterIn.QUERY),
    })
    public Result getAllByConditionByPage(@RequestParam(required = false) String fhName,
                                          Integer pageNum,
                                          Integer pageSize){
        return Result.success(fenceHouseService.getAllByFhNameByPage(fhName, pageNum, pageSize));
    }

    @GetMapping("{fhId}")
    @Operation(summary = "查询指定栏舍(含栏圈)")
    @Parameter(name ="fhId" ,description = "栏舍id",in = ParameterIn.PATH)
    public Result getByIdWithMhs(@PathVariable String fhId){
        return Result.success(fenceHouseService.getDetailByFhId(fhId));
    }

    @PostMapping("/saveOrUpdate")
    @Operation(summary = "新增/修改栏舍数据")
    public Result saveOrUpdate(@RequestBody ManagerFenceHouse managerFenceHouse){
        // 因为可能出现异常，需要处理一下错误返回
        try {
            fenceHouseService.saveOrUpdate(managerFenceHouse);
            return Result.success();
        }catch (ServiceException e){
            return Result.failure(e.getResultCode());
        }
    }

    @DeleteMapping("/{fhId}")
    @Operation(summary = "根据fhId删除指定栏舍")
    public Result remove(@PathVariable String fhId){
        try {
            fenceHouseService.removeById(fhId);
            return Result.success();
        }catch (ServiceException e){
            return Result.failure(e.getResultCode());
        }

    }

    @DeleteMapping("/deleteByIdAll")
    @Operation(summary = "批量删除栏舍")
    public Result removeByIdAll(@RequestBody List<String> ids){
        try {
            fenceHouseService.removeByIdAll(ids);
            return Result.success();
        }catch (ServiceException e){
            return Result.failure(e.getResultCode());
        }
    }

    @GetMapping("/queryAll")
    @Operation(summary = "查询所有栏舍")
    public Result queryAll(){
        return Result.success(fenceHouseService.getAll());
    }
}